package org.com.lcuiot.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import reactor.core.publisher.Mono;

/**
 * 登陆Token所包含信息读取
 *
 * @author gaoge
 * @since 2022/11/19 11:39
 */
public class GetTokenInfo {

    public static Mono<JwtUser> getAuthentication() {
        return ReactiveSecurityContextHolder.getContext()
                .switchIfEmpty(Mono.error(new IllegalStateException("ReactiveSecurityContext is empty")))
                .map(SecurityContext::getAuthentication)
                .map(Authentication::getPrincipal)
                .cast(JwtUser.class);

    }
}
